--[[

创建一个横向选择面板，在里面添加选择按钮 

[] [] []

]]
local function get_real_x(index,max,w,gap)
    local gap = gap or 50
    local x0 = (1920 - max*w - (max-1)*gap)/2
    return x0 + (index-1)*(w+gap)
end

class.select_panel = extends(class.panel) {
    new = function (parent,name)

        local panel = class.select_panel:builder 
        {
            parent = parent,
            x = 0,
            y = 0,
            w = 1,
            h = 1,
            name = name,
        }
        return panel
    end,
    
    add_select_frame = function(self,image,w,h)

        --选择框
        self.select = self:add_panel(image,0,0,w,h)
        self.select:set_level(99)
        self.select:hide()
    end,
    --设置选择按钮
    set_select_button = function (self, count,class_type,select_w,select_h)
        
        self.childs = {}
        self.count = count

        for a=1,count do
            local x = get_real_x(a,count,select_w,50)
            local button = self:add(class_type,x,200,select_w,select_h,1)
            button.button_type = '选择按钮'
            button.index = a
            self.childs[a] = button
        end
    end,


    --显示数据
    set_data = function(self,list,time)
        if self.select then self.select:hide() end
        
        self:show()
        for a,button in ipairs(self.childs) do
            local data = list[a]
            if data then
                local x = get_real_x(a,#list,button.w,50)
                button:set_position(x,button.y)
                button:set_data(data)
                button:show()
                if button:has_mouse() then
                    self:on_button_mouse_enter(button)
                end
            else
                button:hide()
            end
        end
        self.wait_select = ac.clock() + (time or 0)*1000
        self:notify('update')
    end,
    wait_select = 0,

    on_button_clicked = function (self, button)
        if button.index and button.button_type=='选择按钮' then 
            ac.sync(self.sync_name or self.name,button.index)
            
            if self.select then self.select:hide() end
            self:notify('on_select',button)
            self:hide()
        end 
    end,
    on_button_mouse_enter = function(self,button)
        if button.index and button.button_type=='选择按钮' and button.index~=0 then 
            local x,y = button:get_real_position()
            if self.select then 
                self.select:set_real_position(x,y)
                self.select:show()
            end
        end
    end,
    on_button_mouse_leave = function(self)
        if self.select then self.select:hide() end
    end

}

